<template>
  <div class="doingProjectMain">
    <div class="barBox">
      <div class="barItem flex align-center justify-between" v-for="(item, index) in barData" :key="index">
        <div class="leftText flex align-center">
          <span>{{ index + 1 }}</span>
          <div>{{ item.name }}</div>
        </div>
        <div class="rightBar">
          <div :style="{ width: (item.number / totalNumber).toFixed(2) * 100 + '%' }">
            <span>{{ item.number }}</span>
          </div>
        </div>
      </div>
    </div>
    <div id="lineChart"></div>
  </div>
</template>
<script>
import * as echarts from "echarts";
let timer = null;
export default {
  name: 'PerformanceTrend',
  data() {
    return {
      totalNumber: 626,
      barData: [
        {
          id: 1,
          name: '农行灯谜会',
          number: 224
        },
        {
          id: 2,
          name: '悠然牧业',
          number: 153
        },
        {
          id: 3,
          name: '港口集团',
          number: 102
        },
        {
          id: 4,
          name: '科技局',
          number: 84
        },
        {
          id: 5,
          name: '中电',
          number: 63
        }
      ],
      flag: true,
      xAxisData: ['陈宁', '张越', '宁晓冰', '温雅鑫', '郑佳宝'],
      yAxisData: [120, 200, 150, 80, 70]
    }
  },
  mounted() {
    this.initChart();
    timer = setInterval(() => {
      if (this.flag) {
        this.yAxisData = [120, 200, 150, 80, 70];
        this.flag = false;
      } else {
        this.yAxisData = [70, 120, 200, 150, 80]
        this.flag = true;
      }
      this.initChart();
    }, 4000);
  },
  beforeDestroy() {
    clearInterval(timer);
  },
  methods: {
    initChart() {
      let dom = document.getElementById('lineChart');
      let chart = echarts.init(dom, 'chalk')
      let option = {
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          top: '8%',
          left: '5%',
          right: '5%',
          bottom: '0',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: this.xAxisData,
          axisLine: {
            lineStyle: {
              color: "#fff",
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            lineStyle: {
              color: "#fff",
            }
          }
        },
        series: [
          {
            data: this.yAxisData,
            type: 'bar',
            color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
              {
                offset: 0,
                color: '#83bff6'
              },
              {
                offset: 1,
                color: '#11d56d'
              }
            ]),
            label: {
              show: true,
              position: 'top', // 顶部显示
              formatter: '{c}', // 显示数据值
              textStyle: {
                color: '#fff'
              }
            },
          }
        ]
      };
      chart.setOption(option)
      // 监听屏幕变化自动缩放图表
      let observer = new ResizeObserver(function () {
        chart.resize();
      });
      observer.observe(dom)
    }
  }
}
</script>
<style lang="scss" scoped>
.doingProjectMain {
  width: 100%;
  height: 100%;
  position: relative;
  z-index: 99;

  .barBox {
    height: 160px;

    .barItem {
      height: 30px;
      width: 100%;
      padding: 0 30px;
      box-sizing: border-box;

      .leftText {
        width: 40%;

        span {
          display: block;
          width: 20px;
          height: 20px;
          text-align: center;
          line-height: 20px;
          background: #f33;
          font-size: 14px;
          color: #fff;
          margin-right: 10px;
        }

        div {
          font-size: 14px;
          color: #fff;
        }
      }

      .rightBar {
        width: 50%;
        height: 20px;
        background: rgba(255, 255, 255, 0.3);
        position: relative;

        >div {
          height: 20px;
          background: #f33;
          position: absolute;
          right: 0;
          top: 0;
          text-align: right;
          transition: all 0.2s;
          width: 0;

          span {
            margin-right: 10px;
            color: #fff;
            font-size: 12px;
            line-height: 20px;
          }
        }
      }
    }
  }

  #lineChart {
    height: calc(100% - 160px);
  }
}
</style>
